#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e6 + 5;

int n, m;
char s[N];
int pos[N][26];
int cnt[N];
ll ans;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> s[i];
  rep(c, 'a', 'z') pos[n + 1][c - 'a'] = n + 1;
  per(i, n, 1) {
    int j = pos[i + 1][s[i] - 'a'];
    if (j <= n) {
      rep(c, 'a', 'z') pos[i][c - 'a'] = pos[j + 1][c - 'a'];
      cnt[i] = 1 + cnt[j + 1];
    } else {
      rep(c, 'a', 'z') pos[i][c - 'a'] = n + 1;
      cnt[i] = 0;
    }
    pos[i][s[i] - 'a'] = i;
  }
  ans = accumulate(cnt + 1, cnt + n + 1, 0ll);
  cout << ans;
  return 0;
}